// pages/mc.js
const mcService = require('../../service/mc.services.js')

//获取应用实例
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    bannerHeight: 0,
    friendRightWidth: 0,
    picHeight: 0,
    mc: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //隐藏分享
    wx.hideShareMenu();

    var that = this;

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
    }
    mcService.getMcList({
      app: app,
      pageIndex: 1,
      pageSize: 10,
      success: function (res) {
        that.setData({
          mc: res
        })
      }
    });


    var res = wx.getSystemInfoSync();
    this.setData({
      friendRightWidth: res.windowWidth - 50,
      picHeight: res.windowWidth * 0.24
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 图片加载
   */
  imgLoad: function (e) {
    var res = wx.getSystemInfoSync();
    var imgWidth = e.detail.width;
    var imgHeight = e.detail.height;
    var ratio = imgWidth / imgHeight;
    this.setData({
      bannerHeight: res.windowWidth / ratio
    })
  },
  /**
   * 复制文案
   */
  copyContent: function(e) {
    var content = e.currentTarget.dataset.content;
    wx.setClipboardData({
      data: content,
      success: function() {
        wx.showToast({
          title: '文案复制成功',
          icon: 'success',
          duration: 1000
        })
      },
      fail: function() {
        wx.showToast({
          title: '文案复制失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 图片保存
   */
  saveImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var img = [];

    if (this.data.mc.records[index].mcImg1 != null) {
      img.push(this.data.mc.records[index].mcImg1);
    }

    if (this.data.mc.records[index].mcImg2 != null) {
      img.push(this.data.mc.records[index].mcImg2);
    }

    if (this.data.mc.records[index].mcImg3 != null) {
      img.push(this.data.mc.records[index].mcImg3);
    }

    if (this.data.mc.records[index].mcImg4 != null) {
      img.push(this.data.mc.records[index].mcImg4);
    }

    if (this.data.mc.records[index].mcImg5 != null) {
      img.push(this.data.mc.records[index].mcImg5);
    }

    if (this.data.mc.records[index].mcImg6 != null) {
      img.push(this.data.mc.records[index].mcImg6);
    }

    if (this.data.mc.records[index].mcImg7 != null) {
      img.push(this.data.mc.records[index].mcImg7);
    }

    if (this.data.mc.records[index].mcImg8 != null) {
      img.push(this.data.mc.records[index].mcImg8);
    }

    if (this.data.mc.records[index].mcImg9 != null) {
      img.push(this.data.mc.records[index].mcImg9);
    }

    //无图片
    if(img.length == 0) {
      wx.showToast({
        title: '无可保存图片',
        success: function() {
          return ;
        }
      })
    }

    //授权保存相册
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success: function(e) {

        //遍历数组
        img.forEach(function(item, index) {
          wx.showLoading({
            title: '图片保存中……',
            mask: true
          });
          
          //下载文件到本地临时文件
          wx.downloadFile({
            url: item,
            success: function (res) {
              if (res.statusCode >= 200 && res.statusCode < 300) {

                //保存到系统相册
                wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: function (errMsg) {

                    wx.hideLoading();
                    wx.showToast({
                      title: '保存图片成功',
                      icon: 'success'
                    })

                  }
                })
              }
            }
          })
        });

      }
    })
  },
  prewImg: function(e) {
    var index = e.currentTarget.dataset.index;
    var img = e.currentTarget.dataset.img;
    if(img == null) {
      return ;
    }
    var imgs = [];

    if (this.data.mc.records[index].mcImg1 != null) {
      imgs.push(this.data.mc.records[index].mcImg1);
    }

    if (this.data.mc.records[index].mcImg2 != null) {
      imgs.push(this.data.mc.records[index].mcImg2);
    }

    if (this.data.mc.records[index].mcImg3 != null) {
      imgs.push(this.data.mc.records[index].mcImg3);
    }

    if (this.data.mc.records[index].mcImg4 != null) {
      imgs.push(this.data.mc.records[index].mcImg4);
    }

    if (this.data.mc.records[index].mcImg5 != null) {
      imgs.push(this.data.mc.records[index].mcImg5);
    }

    if (this.data.mc.records[index].mcImg6 != null) {
      imgs.push(this.data.mc.records[index].mcImg6);
    }

    if (this.data.mc.records[index].mcImg7 != null) {
      imgs.push(this.data.mc.records[index].mcImg7);
    }

    if (this.data.mc.records[index].mcImg8 != null) {
      imgs.push(this.data.mc.records[index].mcImg8);
    }

    if (this.data.mc.records[index].mcImg9 != null) {
      imgs.push(this.data.mc.records[index].mcImg9);
    }


    //预览
    wx.previewImage({
      urls: imgs,
      current: img
    })

  }
  
})